package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentRepositoryImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室
    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        //1.转化参数类型
        String paramJsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramJsonString, Department.class);
        //2.查询科室信息 根据（hoscode、depcode）
        Department targetDepartment =
                departmentRepository.getByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (targetDepartment!=null){
            //3.记录存在，更新
            department.setId(targetDepartment.getId());
            department.setCreateTime(targetDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(targetDepartment.getIsDeleted());
            departmentRepository.save(department);
        }else{
            //4.记录不存在，新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }
    //带分页查询科室信息
    @Override
    public Page selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //1.创建分页查询对象
        //1.1创建排序对象
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //1.2创建分页对象（第一页为0）
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //2.创建条件查询模板
        //2.1设置筛选条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        //2.2设置模板构造器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //2.3创建条件查询模板
        Example<Department> example = Example.of(department,matcher);
        //3.进行带条件带分页查询
        Page<Department> pageModel = departmentRepository.findAll(example, pageable);
        return pageModel;
    }
    //删除科室
    @Override
    public void removeDepartment(String hoscode, String depcode) {
        //1.根据参数(hoscode,depcode)查询科室信息
        Department department = departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);
        //2.判断科室信息
        if (department==null){
            throw new YyghException(20001,"科室编码有误！");
        }
        //3.根据主键id删除
        departmentRepository.deleteById(department.getId());
    }

    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //1.创建返回集合对象
        List<DepartmentVo> departmentVoList = new ArrayList<>();
        //2.根据医院编号hoscode查询所有科室信息
        List<Department> departmentList = departmentRepository.getByHoscode(hoscode);
        //3.实现根据大科室编码bigcode进行分组
        // List<Department> = Map   k:bigcode    v:List<Department>
        Map<String, List<Department>> depListMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //4.遍历封装大科室信息
        for (Map.Entry<String, List<Department>> entry : depListMap.entrySet()) {
            //4.1创建大科室对象
            DepartmentVo bigDepVo = new DepartmentVo();
            //4.2封装大科室信息
            bigDepVo.setDepcode(entry.getKey());
            bigDepVo.setDepname(entry.getValue().get(0).getBigname());
            //5.封装有关联的小科室信息
            //5.1创建封装小科室信息集合
            List<DepartmentVo> depVoList = new ArrayList<>();
            List<Department> depList = entry.getValue();
            //5.2遍历depList集合进行封装
            for (Department department : depList) {
                DepartmentVo depVo = new DepartmentVo();
                depVo.setDepcode(department.getDepcode());
                depVo.setDepname(department.getDepname());
                depVoList.add(depVo);
            }
            //6.把小科室集合存入大科室对象
            bigDepVo.setChildren(depVoList);
            //7.把大科室对象存入最终返回集合
            departmentVoList.add(bigDepVo);
        }
        return departmentVoList;
    }

    //根据医院编号，科室编号查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    //根据医院编码+科室编码获取科室信息
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department;
        }
        return null;
    }


}
